package net.huashitong.common.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.common.entity.SysBriefing;
import net.huashitong.common.filter.SysBriefingFilter;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SysBriefingDao extends StringDao<SysBriefing> {

    public List<SysBriefing> queryByFilter(SysBriefingFilter filter) {
        GenericQuery query = createQuery("t");
        if (StringUtils.isNotBlank(filter.getType())) {
            query.append(" AND t.type =:type").setParam("type",filter.getType());
        }
        if (StringUtils.isNotBlank(filter.getTitle())) {
            query.append(" AND t.title like :title").setParam("title",StringUtils.getLikewords(filter.getTitle()));
        }
        if (StringUtils.isNotBlank(filter.getContent())) {
            query.append(" AND t.content like :content").setParam("content",StringUtils.getLikewords(filter.getContent()));
        }
        if (StringUtils.isNotBlank(filter.getSummary())) {
            query.append(" AND t.summary like :summary").setParam("summary",StringUtils.getLikewords(filter.getSummary()));
        }
        if (StringUtils.isNotBlank(filter.getStatus())) {
            query.append(" AND t.status =:status").setParam("status",filter.getStatus());
        }
        if (StringUtils.isNotBlank(filter.getDateAttr())){
            query.append(" AND t.dateAttr like :dateAttr").setParam("dateAttr",StringUtils.getLikewords(filter.getDateAttr()));
        }
        if (StringUtils.isNotBlank(filter.getDocType())){
            query.append(" AND t.docType =:docType").setParam("docType",filter.getDocType());
        }
       return query.listResult(filter);
    }

    public List<SysBriefing> checkTitle(SysBriefingFilter filter) {
        GenericQuery query = createQuery("t");
        query.append(" AND t.title = :title").setParam("title",filter.getTitle());
        return query.listResult(filter);
    }


    public List<String> getTypes(){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT DISTINCT type from t_common_briefing");
        SQLQuery query  = getSession().createSQLQuery(sql.toString());
        List<String> types  = (List<String>) query.list();
        return types;
    }
}
